export default {
  namespaced: true,
  state: () => ({
    //书架图书信息
    bookList: JSON.parse(uni.getStorageSync('bookList') || '[]')
  }),
  mutations: {
    addToBookList(state, book) {
      state.bookList.push(book)
      this.commit('m_book/saveBookListToStorage')
    },
    // 根据 Ids 从书架中删除对应的书籍
    removeBookByIds(state, ids) {
    // 确保 ids 是数组
        if (!Array.isArray(ids)) {
          ids = [ids];
        }
        // 过滤掉匹配 id 的书籍
        state.bookList = state.bookList.filter(book => !ids.includes(book.id));
        // 持久化存储到本地
        this.commit('m_book/saveBookListToStorage');
    },
    updateBookList(state, bookList) {
      state.bookList = bookList
      // 通过 this.commit() 方法，调用 m_user 模块下的 saveUserInfoToStorage 方法，将 userinfo 对象持久化存储到本地
      this.commit('m_book/saveBookListToStorage')
    },

    // 将 userinfo 持久化存储到本地
    saveBookListToStorage(state) {
      uni.setStorageSync('bookList', JSON.stringify(state.bookList))
    },
    selectAllBooks(state) {
      state.bookList = state.bookList.map(item => ({
        ...item,
        isSelected: true
      }))
    },
    cancelSelectAllBooks(state){
      state.bookList = state.bookList.map(item => ({
        ...item,
        isSelected: false
      }))
    }
  },
  getters: {}
}
